package cn.edu.dlnu.service.impl;

import cn.edu.dlnu.entity.Answer;
import cn.edu.dlnu.entity.Question;
import cn.edu.dlnu.entity.User;
import cn.edu.dlnu.mapper.AnswerMapper;
import cn.edu.dlnu.mapper.QuestionMapper;
import cn.edu.dlnu.mapper.UserMapper;
import cn.edu.dlnu.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private AnswerMapper answerMapper;
    @Autowired
    private UserMapper userMapper;


    @Override
    public Question findById(String id) {
        return questionMapper.findById(id);
    }

    @Override
    public List<Question> findAll() {
        return questionMapper.findAll();
    }

    @Override
    public List<Question> findQuestionsByTime() {
        List<Question> questions = questionMapper.findQuestionsByTime();
        for (Question question : questions) {
            // 查询到某个问题的最高赞回答
            Answer answer = answerMapper.findTopOneByQno(question.getQno());
            if (answer != null) {
                // 查询某个回答的回答者
                User user = userMapper.findById(answer.getUid());
                answer.setUser(user);
            }
            question.setTopAnswer(answer);
        }
        return questions;
    }
}
